
class NumberRolling 
{
    constructor(text, formatnumberthousands) 
    {
        this.mText          = text
        this.mStartValue    = 0
        this.mTargetValue   = 0
        this.mTime          = 0

        this.mFormatnumberthousands = formatnumberthousands
    }

    start(targetValue, startValue) 
    {
        this.mTargetValue = targetValue

        if (startValue != undefined && startValue != null) 
        {
            this.mStartValue = startValue
            this.setContent(this.mStartValue)
        }
        this.mTime = 1
    }

    update(dt) 
    {
        if (this.mStartValue == this.mTargetValue || this.mTime <= 0 || !this.mText) 
        {
            return
        }

        this.mTime = this.mTime - dt

        if (this.mTime <= 0 || this.mTargetValue <= this.mStartValue) 
        {
            this.mStartValue = this.mTargetValue
            this.setContent(this.mStartValue)
        }
        else 
        {
            let d = Math.floor((this.mTargetValue - this.mStartValue) * dt + 0.5)
          
            this.mStartValue = this.mStartValue + d
            this.setContent(this.mStartValue)
        }
    }

    setContent(value) 
    {
        if (this.mFormatnumberthousands && value != 0) 
        {
            this.mText.getComponent(cc.Label).string = value.toString()
        }
        else 
        {
            this.mText.getComponent(cc.Label).string = value
        }

        if (this.mScalable) 
        {
            if (this.mTargetValue == value) 
            {
                this.mText.stopActionByTag(111)
            }
            else 
            {
                if (!this.mText.getActionByTag(111)) 
                {
                    this.mText.runAction(this.mAction)
                }
            }
        }
    }

    setScalable(value) 
    {
        this.mScalable = value

        if (this.mScalable && !this.mAction) 
        {
            this.mAction = cc.sequence(cc.scaleTo(0.1, 1.1), cc.scaleTo(0.1, 1))
            this.mAction.tag = 111
        }
        else 
        {
            if (this.mAction) 
            {
                this.mAction.stop()
            }
        }
    }

    clear() 
    {
        if (this.mAction) 
        {
            this.mStop()
        }
        this.mText = null
    }
}

export default NumberRolling
